<template>
	<view class="container">

		<view class="user-section">
			<image class="bg" src="/static/user-bg.jpg"></image>
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="userInfo.avatarUrl || '/static/missing-face.png'"></image>
				</view>
				<view class="info-box">
					<text class="username">{{member[0].NiName || userInfo.nickName || '游客'}}</text>
				</view>
				<view v-if="id==0" class="navigator" @click="navTo('/pages/public/login')">去登陆></view>
			</view>
			<view class="vip-card-box">
				<image class="card-bg" src="/static/vip-card-bg.png" mode=""></image>
				<view class="tit">
					<text class="yticon icon-iLinkapp-"></text>
					<!-- DCloud会员 -->
				</view>
				<text class="e-m">DCloud Union</text>
				<!-- <text class="e-b">开通会员开发无bug 一测就上线</text> -->
			</view>
		</view>

		<view class="cover-container" :style="[{
				transform: coverTransform,
				transition: coverTransition
			}]" @touchstart="coverTouchstart" @touchmove="coverTouchmove" @touchend="coverTouchend">
			<image class="arc" src="/static/arc.png"></image>

			<view class="member" :class="{'none': id==0}" :style="{'backgroundImage': 'url('+ BJimg +')'}">
				<text class="member-card">
					<text class="iconfont icon-dianpu card-icon"></text>
					<text>{{member[0].CorpName}}</text>
				</text>
				<text class="member-card">
					<text class="iconfont icon-huiyuan card-icon"></text>
					<text>{{member[0].ClassName}}</text>
				</text>
				<!-- 0折扣卡 1会员价 2零售价 FTypeID-->
				<text class="member-card" v-if="member[0].FTypeID==0">
					<text>折扣:{{member[0].FDiscount}}</text>
					<text></text>
					<text v-if="member[0].FDiscount<=1">(说明:目前会员等级低,暂时没有折扣)</text>
					<text v-else>(说明:目前会员可以打{{member[0].FDiscount}}折)</text>
				</text>
				<text class="member-card" v-else-if="member[0].FTypeID==2">
					<text>折扣:{{member[0].FDiscount}}</text>
					<text></text>
					<text>(说明:目前会员卡,暂时没有折扣)</text>
				</text>
				<text class="member-card" v-else="">
					<!-- <text>会员价:{{member[0].FDiscount}}</text>
					<text></text> -->
					<text>(说明:目前会员可以使用会员价)</text>
				</text>
				<!-- <text class="member-card">
					当前成长值:()
				</text> -->
				<view class="canvas">
					<canvas canvas-id="barcode"></canvas>
				</view>
				<text class="member-card">
					电话号码:<text>{{member[0].Number}}</text>
				</text>
			</view>

			<view class="tj-sction">
				<view class="tj-item" @click="navTo('/pages/record/record?id=0')">
					<text class="num">{{member[0].Amount||0}}</text>
					<text>余额</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/record/record?id=1')">
					<text class="num">{{member[0].Ponit||0}}</text>
					<text>积分</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/coupon/coupon')">
					<text class="num">{{couponNumber}}</text>
					<text>优惠券</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/card/card')">
					<text class="num">{{cardNumber}}</text>
					<text>次卡</text>
				</view>
			</view>
			<!-- 订单 -->
			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-shouye"></text>
					<text>全部订单</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-daifukuan"></text>
					<text>待付款</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-yishouhuo"></text>
					<text>待收货</text>
				</view>
				<!-- <view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>退款/售后</text>
				</view> -->
			</view>
			<!-- 浏览历史 :class="'history-list'" -->
			<view class="history-section icon">
				<list-cell listid="0" icon="yticon icon-iconfontweixin" iconColor="#e07472" title="会员充值"
					@eventClick="navTo('/pages/MemberTopUp/MemberTopUp')"></list-cell>
				<list-cell listid="0" icon="iconfont icon-jiciqia" iconColor="" title="次卡购买"
					@eventClick="navTo('/pages/coupon/timeCard')"></list-cell>
				<list-cell listid="0" icon="yticon icon-dizhi" iconColor="#5fcda2" title="地址管理"
					@eventClick="navTo('/pages/address/address')"></list-cell>
				<!-- <list-cell listid="0" icon="icon-share" iconColor="#9789f7" title="我的会员卡" @eventClick="miniProgram"></list-cell> -->
				<list-cell listid="0" icon="yticon icon-shoucang_xuanzhongzhuangtai" iconColor="#54b4ef" title="我的收藏"
					@eventClick="navTo('/pages/collect/collect')"></list-cell>
				<list-cell listid="0" icon="iconfont icon-lianluojilu" iconColor="" title="推荐有礼"
					@eventClick="navTo('/pages/recommend/recommend')"></list-cell>
				<list-cell listid="0" icon="iconfont icon-qiandao" iconColor="" title="签到有礼"
					@eventClick="navTo('/pages/sign/sign')"></list-cell>
				<list-cell listid="0" icon="iconfont icon-wanshanziliao" iconColor="" title="完善资料"
					@eventClick="navTo('/pages/userinfoSet/userinfoSet')"></list-cell>
			</view>
		</view>
	</view>
</template>
<script>
	import listCell from '@/components/mix-list-cell';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			listCell
		},
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				userInfo: '',
				member: '', //会员信息
				// couponNumber: '',//优惠劵数量
				id: 0,
				BJimg: '',
			}
		},
		onLoad(options) {
			let scene = decodeURIComponent(options.scene)
			let id = uni.getStorageSync('LoginID'); //登录状态码
			let str = scene.split(':');
			options.scene = decodeURIComponent(options.scene)
			options.corpid = 3;
			options.FItemID = str[1];
			options.hostid = str[2];
			options.PagePath = str[3];
			let UserInfo = uni.getStorageSync('MuserInfo'); //用户微信信息
			// let member = uni.getStorageSync('Members');//会员信息
			let token = uni.getStorageSync('Token'); //Token值
			this.$Url = uni.getStorageSync('URL') || this.$util.getUrls();
			this.$corpid = uni.getStorageSync('corpid') || options.corpid;
			this.$hostid = uni.getStorageSync('hostid') || options.hostid;
			uni.setStorageSync('hostid', options.hostid)
			if (uni.getStorageSync('stockId') == null || uni.getStorageSync('stockId') == "") {
				uni.setStorageSync('stockId', options.FItemID)
			}
			this.id = id;
			let phone = uni.getStorageSync('phone');
			if (phone == null || phone == "") {
				// uni.navigateTo({
				// 	url: '/pages/public/login?scene=' + scene
				// })
				return;
			}
			debugger
			uni.showLoading({
				title: '加载中'
			})
			debugger
			this.userInfo = UserInfo;
			this.GetUserInfo();
			let slef = this;
			setInterval(function() {
				slef.GetUserInfo();
			}, 10000);
			// uni.request({
			// 	url: this.$Url + 'sale/CheckTokenLogin',
			// 	data: '',
			// 	method: 'POST',
			// 	header: {
			// 		'token': token
			// 	},
			// 	success: (res) => {
			// 		let hostid = uni.getStorageSync('hostid'); //当前服务器id
			// 		let oldHostid = uni.getStorageSync('Oldhostid'); //上一次服务器id
			// 		let id = uni.getStorageSync('LoginID');
			// 		this.MemberLogin();
			// 		return;
			// 		if (res.data.msg == 'success' && id == 1) {
			// 			/**
			// 			 * 用来判断服务器切换时，提示用户要重新登录
			// 			 * 后端说不用管的，但前台来做，看起来会好点
			// 			 */
			// 			if (hostid != oldHostid && oldHostid !== '') {
			// 				uni.hideLoading();
			// 				uni.showModal({
			// 					title: '登录提示',
			// 					content: '没有记录到登录信息，请重新登录',
			// 					success: (res) => {
			// 						if (res.confirm) {
			// 							uni.navigateTo({
			// 								url: '/pages/public/login'
			// 							})
			// 							uni.setStorageSync('Oldhostid', hostid)
			// 						}
			// 					}
			// 				})
			// 			} else {
			// 				this.userInfo = UserInfo;
			// 				//获取用户会员卡信息
			// 				this.MemberData();
			// 			}
			// 		} else {
			// 			this.id = 0;
			// 			uni.setStorageSync('Oldhostid', hostid)
			// 			this.$api.msg('请重新登录')
			// 		}
			// 	},
			// 	fail: (error) => {
			// 		console.log(error);
			// 	}
			// })
		},
		onShow() {
			let id = uni.getStorageSync('LoginID');
			if (id == 1) {
			}
		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['couponNumber', 'cardNumber'])
		},
		methods: {
			...mapMutations(['onCoupon', 'onCard']),
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				let id = uni.getStorageSync('LoginID');
				if (id != 1) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},
			//跳转到另一个小程序
			miniProgram() {
				/**
				 * 注意: 这里做的是线上版本的，如果要测试本地版本，
				 * 一定要去会员卡小程序哪里把地址改为本地地址才可以
				 * 一定要注意这一点
				 * 
				 * 商城小程序这里不用，根据开发者前面设置的地址来测试，
				 * 线上就线上，本地就本地，只是上面这一点要记得
				 */
				let stockId = uni.getStorageSync('stockId');
				let hostid = uni.getStorageSync('hostid')
				let token = uni.getStorageSync('Token');
				let scan = {
					FItemID: stockId,
					parentID: '',
					hostid: hostid,
					PagePath: 'Member',
				}
				let data = {
						scan: scan,
					},
					url = 'pages/index/index';
				// this.$api.msg('功能暂未开通');
				this.$util.miniProgram(url, data);
			},

			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}
				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			},
			//获取用户会员卡的优惠劵
			MemberUser() {
				let number = 0;
				this.$util.util('MemberShop/CouponList', {}, 'POST').then(res => {
					res.forEach(item => {
						let timestamp = Date.parse(new Date());
						if (item.FStatus == 0) {
							let tiem = parseInt(item.FEndTime.slice(6, item.FEndTime.length - 2));
							if (timestamp < tiem) {
								number++;
							}
						}
					})
					this.onCoupon(number);
					this.MemberCard();
					uni.hideLoading();
				})
			},
			//次卡信息
			MemberCard() {
				this.$util.util('membershop/MemberTimeGroup', '', 'POST').then(res => {
					if (res != null)
						this.onCard(res.FQty);
				}).catch(error => console.log(error));
			},
			//会员卡信息
			MemberData() {
				this.$util.util('MemberShop/GetMemberData', {mSystemAppID:uni.getStorageSync('mSystemAppID')}, 'GET').then(res => {
					this.member = res;
					uni.setStorageSync('Members', res);
					uni.hideLoading();
					this.MemberBJ();
				})
			},
			//会员卡背景图
			MemberBJ() {
				let data = {
					corpid: this.$corpid, //公司id
					hostid: this.$hostid,
				}
				this.$util.util('WeChatShop/SystemParam', data, 'POST').then(res => {
					this.BJimg = res.FMiniBGK;
					let MemberCard = uni.getStorageSync('Members');
					this.$canvas.barcode('barcode', MemberCard[0].Number, 480, 380);
					this.MemberUser();
				}).catch(error => console.log(error))
			},
			//登录
			MemberLogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						let code = res.code;
						//抛异常
						try {
							this.$util.Login(code).then(res => {
								uni.setStorageSync('LoginID', res)
								this.PhoneID = uni.getStorageSync('PhoneID');
								this.userInfo = uni.getStorageSync('userInfo');
								this.id = 1;
								//获取用户会员卡信息
								this.MemberData();
								if (res == 1 && this.PhoneID == 1) {
									uni.hideLoading();
									// uni.reLaunch({
									// 	url:'../user/user',
									// })

								} else {
									uni.hideLoading();
								}
							});
						} catch (e) {
							console.log(e);
						}
					}
				})
			},
			GetUserInfo(data) {
				this.$util.get('MemberShop/GetMemberData',{mSystemAppID:uni.getStorageSync('mSystemAppID')}, 'GET').then(res => {
					this.member = res.data;
					uni.setStorageSync('Members', res.data);
					uni.hideLoading();
					this.MemberBJ();
				})
			},
		}
	}
</script>
<style lang='scss'>
	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	page {
		background-color: #f5f5f5;
	}

	.user-section {
		height: 520upx;
		padding: 100upx 30upx 0;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			font-size: $font-lg + 6upx;
			color: $font-color-dark;
			margin-left: 20upx;
		}
	}

	.vip-card-box {
		display: flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0, 0, 0, .7), rgba(0, 0, 0, .8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;

		.card-bg {
			position: absolute;
			top: 20upx;
			right: 0;
			width: 380upx;
			height: 260upx;
		}

		.b-btn {
			position: absolute;
			right: 20upx;
			top: 16upx;
			width: 132upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 22upx;
			color: #36343c;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
		}

		.tit {
			font-size: $font-base+2upx;
			color: #f7d680;
			margin-bottom: 28upx;

			.yticon {
				color: #f6e5a3;
				margin-right: 16upx;
			}
		}

		.e-b {
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}

	.cover-container {
		background: $page-color-base;
		margin-top: -150upx;
		padding: 0 30upx;
		position: relative;
		background: #f5f5f5;
		padding-bottom: 20upx;

		.arc {
			position: absolute;
			left: 0;
			top: -34upx;
			width: 100%;
			height: 36upx;
		}
	}

	.tj-sction {
		@extend %section;

		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #75787d;
		}

		.num {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}

	.order-section {
		@extend %section;
		padding: 28upx 0;
		margin-top: 20upx;

		.order-item {
			@extend %flex-center;
			width: 120upx;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;
		}

		.yticon {
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #fa436a;
		}

		.icon-shouhoutuikuan {
			font-size: 44upx;
		}
	}

	.history-section {
		padding: 30upx 0 0;
		margin-top: 20upx;
		background: #fff;
		border-radius: 10upx;

		.sec-header {
			display: flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;

			.yticon {
				font-size: 44upx;
				color: #5eba8f;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}

		.h-list {
			white-space: nowrap;
			padding: 30upx 30upx 0;

			image {
				display: inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}
	}

	.history-list {
		display: flex;
		-webkit-box-align: center;
		-ms-flex-align: center;
		align-items: center;
		-ms-flex-wrap: wrap;
		flex-wrap: wrap;

		list-cell {
			width: 25%;
		}
	}

	.navigator {
		font-size: 38upx;
		color: #fa436a;
		margin-left: 100upx;
	}

	.member {
		padding: 0 30upx;
		height: 390upx;
		border-radius: 30upx;
		margin-bottom: 20upx;
		display: flex;
		flex-wrap: wrap;

		.member-card {
			width: 100%;
			height: 42upx;
			line-height: 42upx;
			display: inline-block;

			.card-icon {
				margin-right: 14upx;
			}
		}

		.member-card:first-child {
			margin-top: 34upx;
		}

		.canvas {
			width: 500upx;
			height: 104upx;
			background-color: #fff;
			margin: auto;
		}

		canvas {
			width: 500upx;
			height: 104upx;
		}
	}
</style>
